<?php

namespace App\Http\Controllers;

use App\Models\CollPlace;
use Illuminate\Http\Request;
use Cache;
use Illuminate\Support\Facades\DB;


class PlaceController extends Controller
{
    /**
     * 获取地标信息
     * @return array
     * $pid:当前县级市的id
     */
    public function place(Request $request)
    {
        $place = CollPlace::getPlace($request->pid);
        return response()->json($place);
    }

    public function getLocation($address)
    {
        $auto_navi = $tencent = $baidu = '';
        //高德地图
        $gateway1 = "https://restapi.amap.com/v3/geocode/geo?address={$address}&output=XML&key=0bd909ecdf050f26c0e2ef1bc4d79263";
        $result1 = file_get_contents($gateway1);
        $result10 = json_decode(json_encode(simplexml_load_string($result1, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

        if ($result10['status'] == 1) {
            if (isset($result10['geocodes']['geocode'])) {
//                return response()->json(['status' => 0, 'msg' => '详细地址错误！']);
                $location = $result10['geocodes']['geocode']['location'];
                $locationArr = explode(',', $location);
                $auto_navi = $locationArr[0] . ',' . $locationArr[1];
            }
        }
        //腾讯地图
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?address={$address}&key=VUHBZ-42PAP-EMLD3-VCORN-OO2J5-FIF2P";
        $wechat = new WechatController();
        $result20 = $wechat->get_curl($url);

        if ($result20 && $result20['status'] == 0) {
            $lng = $result20['result']['location']['lng'];
            $lat = $result20['result']['location']['lat'];
            if ($lng && $lat) {
                $tencent = $lng . ',' . $lat;
            }
        }

        //百度地图
        $gateway3 = "https://api.map.baidu.com/geocoding/v3/?address={$address}&output=xml&ak=YA9Z6GkeGd1yQFSqfSwsgKLfn4WOrjhd";
        $result3 = file_get_contents($gateway3);

        $result30 = json_decode(json_encode(simplexml_load_string($result3, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if ($result30['status'] == 0) {
            $location = $result30['result']['location'];
            if (isset($location)) {
                $baidu = $location['lng'] . ',' . $location['lat'];
            }
        }

        if ($auto_navi && $tencent && $baidu) {
            $data = [
                'auto_navi' => $auto_navi,
                'tencent' => $tencent,
                'baidu' => $baidu,
            ];
            $id = DB::table('latlng_info')->insertGetId($data);
            if ($id) {
                return $id;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 腾讯开放平台
     * @param $lat
     * @param $lng
     * @return mixed
     */
    public function getLocationTX($lat, $lng)
    {
        $gateway = "https://apis.map.qq.com/ws/geocoder/v1/?location={$lat},{$lng}&output=json&key=VUHBZ-42PAP-EMLD3-VCORN-OO2J5-FIF2P&get_poi=1";
        $result = file_get_contents($gateway);

        $position1 = strpos($result, 'city');
        $position2 = strpos($result, 'district');
        $str = substr($result, $position1 + 8, $position2 - ($position1 - 8) - 32);

        return $str;
    }
}